package com.right.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Tools.PageData;
import com.commons.AjaxResponse;
import com.google.gson.Gson;
import com.right.dao.FoodsDao;
import com.right.dao.impl.FoodsDaoImpl;
import com.right.entity.Foods;
import com.right.entity.Store;
import com.right.service.FoodsService;
import com.right.service.impl.FoodsServiceImpl;

/**
 * Servlet implementation class FoodsServlet
 */
@WebServlet("/FoodsServlet")
public class FoodsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	// 接口引用指向实现类对象
	FoodsDao fDao = new FoodsDaoImpl();
	FoodsService fService = new FoodsServiceImpl();
	Foods foods = new Foods();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FoodsServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("add".equals(op)) {
			// 添加菜品
			addFoods(request, response);
		} else if ("delete".equals(op)) {
			// 删除菜品
			doDeleteFoods(request, response);
		} else if ("del".equals(op)) {
			doAjaxDel(request, response);
		} else if ("update".equals(op)) {
			// 修改菜品
			doUpdateFoods(request, response);
		} else if ("selectByName".equals(op)) {
			// 根据关键字查询
			doSelectByName(request, response);
		} else if ("selectByPage".equals(op)) {
			// 分页查询菜品
			doSelectByPage(request, response);
		} else if ("selectStoreByFoodsName".equals(op)) {
			doslelectStoreByFoodsName(request, response);
		} else if ("doSelectByFoodsid".equals(op)) {
			doSelectByFoodsid(request, response);
		} else if ("".equals(op)) {
			response.sendRedirect("main.jsp");
		} else if ("doSelectPageByStore".equals(op)) {
			doSelectPageByStore(request, response);
		} else if ("".equals(op)) {
			String keywords = "";
			// 2获取请求参数
			if (request.getParameter("keywords") != null) {
				keywords = request.getParameter("keywords");
			}
			List<Foods> list = fService.getFoods(keywords);
			System.out.println(list.size());
			request.setAttribute("list", list);
			request.getRequestDispatcher("s-foods.jsp").forward(request, response);
		}else if ("sel".equals(op)) {
			doSelect(request, response);
		}
	}

	private void doAjaxDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");

		// 先获取请求参数的值
		int foodsid = 0;

		if (request.getParameter("foodsid") != null) {
			foodsid = Integer.parseInt(request.getParameter("foodsid"));
		}

		// 2 调用 service的方法
		boolean flag = fService.delFoods(foodsid);

		AjaxResponse res = new AjaxResponse(flag + "", 200);
		// 3将list转换为json格式的字符串
		Gson gson = new Gson();
		String jsonString = gson.toJson(res);

		// 4 响应
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.print(jsonString);

		// 5.关闭
		outPrintWriter.close();
	}

	/**
	 * 根据菜品名称模糊查询店名
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doslelectStoreByFoodsName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		System.out.println(keywords);
		// 调用 dao的方法
		Store store = fDao.getStoreByFoodsName(keywords);
		System.out.println(store.getStore());
//		if("肯德基3".equals(store.getStore())) {
		request.setAttribute("store", store.getStore());
		request.getRequestDispatcher("shop.jsp").forward(request, response);
//		}else if("肯德基2".equals(store.getStore())) {
//			request.setAttribute("store", store.getStore());
//			request.getRequestDispatcher("shop.jsp").forward(request, response);
//		}
//		//传递list给jsp
//		request.setAttribute("store", store);
//		//转发
//		request.getRequestDispatcher("#").forward(request, response);
	}

	/**
	 * 修改菜品的方法
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doUpdateFoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取请求的参数
		String foodsname = request.getParameter("foodsname");
		double price = Double.parseDouble(request.getParameter("price"));
		int typeid = Integer.parseInt(request.getParameter("typeid"));
		int storeid = Integer.parseInt(request.getParameter("storeid"));
		String foodsstatus = request.getParameter("foodsstatus");
		int foodsid = Integer.parseInt(request.getParameter("foodsid"));
		// 构造一个foods对象
		foods = new Foods(foodsid, foodsname, price, typeid, storeid, foodsstatus);
		System.out.println(foods);
		boolean flag = fService.updateFoods(foods);
		System.out.println(flag);
		// 根据flag的结果进行转发或者提示
		if (flag) {
			// 转发到查询菜品的界面
			request.setAttribute("msg", "修改成功！");
			request.getRequestDispatcher("s-foods.jsp").forward(request, response);
//				doSelectByPage(request, response);
		} else {
			// 修改失败给提示转发回到修改的页面
			request.setAttribute("msg", "修改失败！");
			request.getRequestDispatcher("s-foods.jsp").forward(request, response);
		}

	}

	/**
	 * 删除菜品
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doDeleteFoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 删除根据编号来进行
		int foodsid = Integer.parseInt(request.getParameter("foodsid"));
		boolean flag = fService.delFoods(foodsid);
		// 根据flag的结果进行转发或者提示
		if (flag) {
			// 转发到查询菜品的界面
			request.setAttribute("msg", "删除成功！");
			doSelectByPage(request, response);
		} else {
			// 添加失败给提示转发回到增加的页面
			request.setAttribute("msg", "删除失败！");
			request.getRequestDispatcher("#").forward(request, response);
		}
	}

	/**
	 * 根据关键字查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doSelectByName(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";
		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		System.out.println(keywords);
		// 调用 dao的方法
		@SuppressWarnings("unchecked")
		List<Foods> list = (List<Foods>) fDao.getOneFoods(keywords);
		// 传递list给jsp
		request.setAttribute("list", list);
		// 转发
		request.getRequestDispatcher("#").forward(request, response);
	}

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doSelectByPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";

		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认值在第一页
		int page = 1;
		// 每页默认10条记录
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 调用通过菜品名查询菜品的Service方法
		PageData<Foods> pData = fService.getFoodsPage(page, pageSize, keywords);
		// 传递PageData对象给jsp
		request.setAttribute("pData", pData);
		// 转发回来的时候，将查询关键字也存储在request范围内
		request.setAttribute("keywords", keywords);
		// 转发
		request.getRequestDispatcher("s-foods.jsp").forward(request, response);

	}
	
	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doSelect(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String keywords = "";

		// 获取请求参数
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		// 页码默认值在第一页
		int page = 1;
		// 每页默认10条记录
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 调用通过菜品名查询菜品的Service方法
		PageData<Foods> pData = fService.getFoodsPage(page, pageSize, keywords);
		// 传递PageData对象给jsp
		request.setAttribute("pData", pData);
		// 转发回来的时候，将查询关键字也存储在request范围内
		request.setAttribute("keywords", keywords);
		// 转发
		request.getRequestDispatcher("shop.jsp").forward(request, response);

	}
	
	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doSelectPageByStore(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String store = "麦当劳";
		if (request.getParameter("store") != null) {
			store = request.getParameter("store");
		}
		// 页码默认值在第一页
		int page = 1;
		// 每页默认10条记录
		int pageSize = 10;
		if (request.getParameter("page") != null) {
			page = Integer.parseInt(request.getParameter("page"));
		}

		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}

		// 调用通过商家名查询菜品的方法
		PageData<Foods> pData = fDao.getFoodsByStorePage(page, pageSize, store);
		request.setAttribute("pData", pData);
		request.setAttribute("store", store);
		request.getRequestDispatcher("shop.jsp").forward(request, response);

	}

	/**
	 * 添加菜品
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void addFoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取请求的参数
		String foodsname = request.getParameter("foodsname");
		double price = Double.parseDouble(request.getParameter("price"));
		int typeid = Integer.parseInt(request.getParameter("typeid"));
		int storeid = Integer.parseInt(request.getParameter("storeid"));
		String foodsstatus = request.getParameter("foodsstatus");

		// 构造一个foods对象
		foods = new Foods(foodsname, price, typeid, storeid, foodsstatus);
		System.out.println(foods);
		boolean flag = fService.addFoods(foods);
		System.out.println(flag);
		// 根据flag的结果进行转发或者提示
		if (flag) {
			// 转发到查询菜品的界面
			doSelectByPage(request, response);
		} else {
			// 添加失败给提示转发回到增加的页面
			request.setAttribute("msg", "增加失败！请重新添加。");
			request.getRequestDispatcher("#").forward(request, response);
		}

	}

	private void doSelectByFoodsid(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int foodsid = 0;
		// 获取请求参数
		if (Integer.parseInt(request.getParameter("foodsid")) != 0) {
			foodsid = Integer.parseInt(request.getParameter("foodsid"));
		}
		System.out.println(foodsid);
		// 调用 dao的方法
		Foods foods = fDao.getFoodsByFoodsid(foodsid);
		System.out.println(foods);
		// 传递foods给jsp
		request.setAttribute("foods", foods);
		// 转发
		request.getRequestDispatcher("product.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
